package cn.m1c.rocket;
 
import java.util.Calendar;
import java.util.List;

import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.alibaba.rocketmq.common.protocol.heartbeat.MessageModel;
 
public class PushConsumer {
         /**
          * 当前例子是PushConsumer用法，使用方式给用户感觉是消息从RocketMQ服务器推到了应用客户端。<br>
          * 但是实际PushConsumer内部是使用长轮询Pull方式从MetaQ服务器拉消息，然后再回调用户Listener方法<br>
          * 生产的数据在消费的时候必须要在consumer端进行幂等判断（即消费端去重）
          */
         public static void main(String[] args) throws InterruptedException,
                            MQClientException{
                   /**
                    * 一个应用创建一个Consumer，由应用来维护此对象，可以设置为全局对象或者单例<br>
                    * 注意：ConsumerGroupName需要由应用来保证唯一
                    */
                   DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("m1cconsumergroup");
                   consumer.setNamesrvAddr("testmq.m1c.cn:9876");
                   consumer.setInstanceName("m1cconsumber"+Calendar.getInstance().getTimeInMillis());
                   consumer.setMessageModel(MessageModel.CLUSTERING);
//                   consumer.setConsumeThreadMin(1);
//                   consumer.setConsumeThreadMax(1);
 
                   /**
                    * 订阅指定topic下tags分别等于TagA或TagC或TagD
                    */
//                   consumer.subscribe("m1crocketmqdemo","shoporder || TagC || TagD");
                   consumer.subscribe("m1crocketmqdemo","shoporder");
                   /**
                    * 订阅指定topic下所有消息<br>
                    * 注意：一个consumer对象可以订阅多个tag
                    */
//                   consumer.subscribe("TopicTest2","*");
 
                   consumer.registerMessageListener(new MessageListenerConcurrently() {
 
                            public ConsumeConcurrentlyStatus consumeMessage(
                                               List<MessageExt>msgs, ConsumeConcurrentlyContext context) {
 
                                     System.out.println(Thread.currentThread().getName()
                                                        +" Receive New Messages: " + msgs.size());
                                     for(int i=0;i<msgs.size();){
                                    	 MessageExt msg = msgs.get(i);
                                    	 if(msg.getTopic().equals("m1crocketmqdemo")) { 
                                        	 System.out.println("处理成功");
                                        	 System.out.println(new String(msg.getBody()));
                                        	 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                                         }else{
                                        	 System.out.println("处理失败");
                                        	 return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                                         }
                                     }
                                     return ConsumeConcurrentlyStatus.RECONSUME_LATER;
 
                                     
 
                            }
                   });
 
                   /**
                    * Consumer对象在使用之前必须要调用start初始化，初始化一次即可<br>
                    */
                   consumer.start();
                   System.out.println("ConsumerStarted.");
         }
}